﻿using Sunny.UI;
using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.ComponentModel.DataAnnotations;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Security.Cryptography;
using System.Text;
using System.Threading.Tasks;
using System.Windows.Forms;
using WinFormsApp1.Common;
using static WinFormsApp1.TongShiWei.segmentationData.DepotData_;

namespace WinFormsApp1.TongShiWei.segmentationData
{
    public partial class SegmentationContext : UIForm
    {
        public SegmentationContext(string DatetimeNow)
        {
            InitializeComponent();
            // 设置窗口的最小和最大尺寸相同，以固定窗口大小
            this.MinimumSize = new Size(1500, 1100); // 设定窗口的最小尺寸
            this.MaximumSize = new Size(1500, 1100); // 设定窗口的最大尺寸

            // 设置窗口边框样式为固定单边框
            this.FormBorderStyle = FormBorderStyle.FixedSingle;

            uiTextBox1.Text = DatetimeNow;

            GetUPCData();
            //显示分割称重入库表数据
            ShowRKWeighData();
        }
        #region 商品代号显示
        List<Button> buttons = new List<Button>();
        //商品代号+分割领用数据
        List<UPCData> uPCDatas = new List<UPCData>();
        int pid = 0;
        int ynum = 1;
        int qnum = 12;
        int znum = 0;
        int count = 0;
        public async void GetUPCData()
        {
            GetUpcTableData getUpcTableData = new GetUpcTableData()
            {
                GoodsMinuteId = 0,
                UPCDataName = ""
            };
            if (buttons.Count > 0)
            {
                foreach (Button button in buttons)
                {
                    this.Controls.Remove(button);
                }
                buttons.Clear();
            }
            //http://localhost:5106/api/Tsw/ShowSplitnockData
            var res = await HttpClientHelper.RequestUrl<GetUpcTableData, ApiResult<List<UPCData>>>(URLCommon.Url2, "/TSW/ShowSplitnockData", HttpClientHelper.MethodType.GET);
            if (res.Code > 0)
            {
                uPCDatas = res.Data;
                if (pid > 0)
                {
                    uPCDatas = uPCDatas.Where(x => x.CommodityId == pid).ToList();
                }
                count = uPCDatas.Count();
                znum = Convert.ToInt32(Math.Ceiling((double)count / qnum));
                var UpcTableData_ = uPCDatas.OrderBy(x => x.GoodsMinuteId).Skip((ynum - 1) * qnum).Take(qnum).ToList();
                int x = 120; int y = 377; int i = 1;
                foreach (var item in UpcTableData_)
                {
                    Button radioButton = new Button()
                    {
                        Size = new System.Drawing.Size(170, 120),
                        BackColor = Color.Gainsboro,
                        //RadioButtonColor = Color.Transparent,
                        ForeColor = Color.Black,
                        Text = item.UPCDataName,
                        Name = item.GoodsMinuteId.ToString()
                    };
                    radioButton.Location = new System.Drawing.Point(x, y);
                    radioButton.TextAlign = ContentAlignment.MiddleCenter;
                    radioButton.AutoSize = true;
                    if (i == 3)
                    {
                        x = 120;
                        y += 130;
                        i = 1;
                    }
                    else
                    {
                        x += 180;
                        i++;
                    }
                    buttons.Add(radioButton);
                    radioButton.Click += new System.EventHandler(Redioclick);
                    this.Controls.Add(radioButton);
                }
            }
        }
        #region 商品分类点击
        /// <summary>
        /// 全部
        /// </summary>
        /// <param name="sender"></param>
        /// <param name="e"></param>
        private void uiButton23_Click(object sender, EventArgs e)
        {
            ynum = 1;
            qnum = 12;
            pid = 0;
            GetUPCData();
        }
        /// <summary>
        /// 鲜段体
        /// </summary>
        /// <param name="sender"></param>
        /// <param name="e"></param>
        private void uiButton18_Click(object sender, EventArgs e)
        {
            ynum = 1;
            qnum = 12;
            pid = 11;
            GetUPCData();
        }
        /// <summary>
        /// 鲜分割
        /// </summary>
        /// <param name="sender"></param>
        /// <param name="e"></param>
        private void uiButton20_Click(object sender, EventArgs e)
        {
            ynum = 1;
            qnum = 12;
            pid = 12;
            GetUPCData();
        }
        /// <summary>
        /// 冻分割
        /// </summary>
        /// <param name="sender"></param>
        /// <param name="e"></param>
        private void uiButton19_Click(object sender, EventArgs e)
        {
            ynum = 1;
            qnum = 12;
            pid = 13;
            GetUPCData();
        }
        /// <summary>
        /// 冻骨
        /// </summary>
        /// <param name="sender"></param>
        /// <param name="e"></param>
        private void uiButton22_Click(object sender, EventArgs e)
        {
            ynum = 1;
            qnum = 12;
            pid = 14;
            GetUPCData();
        }
        /// <summary>
        /// 鲜骨
        /// </summary>
        /// <param name="sender"></param>
        /// <param name="e"></param>
        private void uiButton21_Click(object sender, EventArgs e)
        {
            ynum = 1;
            qnum = 12;
            pid = 15;
            GetUPCData();
        }
        /// <summary>
        /// 上一页
        /// </summary>
        /// <param name="sender"></param>
        /// <param name="e"></param>
        private void uiButton24_Click(object sender, EventArgs e)
        {
            if (ynum >= 2)
            {
                ynum = ynum - 1;
            }
            else
            {
                MessageBox.Show("已经是第一页了");
            }
            GetUPCData();
        }
        /// <summary>
        /// 下一页
        /// </summary>
        /// <param name="sender"></param>
        /// <param name="e"></param>
        private void uiButton26_Click(object sender, EventArgs e)
        {
            if (ynum == znum)
            {
                MessageBox.Show("已经是最后一页了");
            }
            else
            {
                ynum = ynum + 1;
            }
            GetUPCData();
        }
        #endregion
        public class UPCData
        {
            /// <summary>
            /// 分割领用称重Id
            /// </summary>
            public int SplitRequisId { get; set; }

            /// <summary>
            /// 称重时间
            /// </summary>
            public DateTime? SplitRequisTime { get; set; }

            /// <summary>
            /// 仓库
            /// </summary>
            public int DepotId { get; set; }

            /// <summary>
            /// 商品批号
            /// </summary>
            public int ProductId { get; set; }

            /// <summary>
            /// 商品代号
            /// </summary>
            public int MarkId { get; set; }

            /// <summary>
            /// 件数
            /// </summary>
            public string SplitRequisNum { get; set; }

            /// <summary>
            /// 毛重
            /// </summary>
            public string SplitRequisGrossWeight { get; set; }

            /// <summary>
            /// 皮重
            /// </summary>
            public string SplitRequisTares { get; set; }

            /// <summary>
            /// 净重
            /// </summary>
            public string SplitRequisWeight { get; set; }

            /// <summary>
            /// 备注
            /// </summary>
            public string SplitRequisRemark { get; set; }

            /// <summary>
            /// 是否删除
            /// </summary>
            public bool IsDelete { get; set; } = false;
            /// <summary>
            /// 商品代号ID
            /// </summary>
            public int GoodsMinuteId { get; set; }
            /// <summary>
            /// 商品分类Id
            /// </summary>
            public int CommodityId { get; set; }
            /// <summary>
            /// 商品代号名称
            /// </summary>
            public string UPCDataName { get; set; } = string.Empty;
        }
        public class GetUpcTableData
        {
            /// <summary>
            /// 商品代号ID
            /// </summary>
            public int GoodsMinuteId { get; set; }
            /// <summary>
            /// 商品代号名称
            /// </summary>
            public string UPCDataName { get; set; } = string.Empty;
        }
        #endregion
        #region 自适应窗口
        //private new AutoAdaptWindowsSize AutoSize;
        //protected override CreateParams CreateParams
        //{
        //    get
        //    {
        //        CreateParams cp = base.CreateParams;
        //        cp.ExStyle |= 0x02000000;
        //        return cp;
        //    }
        //}

        //private void SegmentationDomain_Load(object sender, EventArgs e)
        //{
        //    AutoSize = new AutoAdaptWindowsSize(this);
        //}

        //private void SegmentationDomain_SizeChanged(object sender, EventArgs e)
        //{
        //    //窗体大小改变事件
        //    if (AutoSize != null) // 一定加这个判断，电脑缩放布局不是100%的时候，会报错
        //    {
        //        AutoSize.FormSizeChanged();
        //    }
        //}
        #endregion



        #region 获取仓库
        int DepotId_Now = 0;
        private void uiButton2_Click(object sender, EventArgs e)
        {
            DepotData_ depotData_ = new DepotData_();
            depotData_.GetDepotIdData += GetDepotData;
            depotData_.Show();
        }
        public void GetDepotData(int DepotId_)
        {
            DepotId_Now = DepotId_;
        }
        #endregion

        #region 获取磅秤值
        int ScaleId_ = 0;
        private void uiButton1_Click(object sender, EventArgs e)
        {
            if (DepotId_Now <= 0)
            {
                MessageBox.Show("还没有选择仓库");
            }
            else
            {
                ScaleData_ scaleData_ = new ScaleData_(DepotId_Now);
                scaleData_.GetScaleIdData_ += GetScaleId;
                scaleData_.Show();
            }
        }
        public void GetScaleId(int ScaleId)
        {
            ScaleId_ = ScaleId;
        }
        #endregion
        public void SegmentationContext_ValeSubmitted(object? sender, valueEventAge e)
        {
            DepotId_Now = Convert.ToInt32(e.value);
        }

        #region 包装
        private void uiButton16_Click(object sender, EventArgs e)
        {
            PackageData packageData = new PackageData();
            packageData.GetPackageData_ += GetPackageDatas;
            packageData.Show();
        }
        //包装数据
        List<string> PackageData_ = new List<string>();
        //包装总重量
        string PackageZkg_ = "";
        //获取选择的包装数据
        public async void GetPackageDatas(List<string> PackageData, string PackageZkg)
        {
            PackageData_ = PackageData;
            PackageZkg_ = PackageZkg;
            this.Pnum.Text = (Convert.ToDouble(this.Pnum.Text) + Convert.ToDouble(PackageZkg_)).ToString();
        }
        #endregion

        #region 计算机
        #region 选择需要添加的数据
        //定义需要添加值的代表字段
        string NumTypeData = "";
        //皮重
        private void Pnum_Click(object sender, EventArgs e)
        {
            NumTypeData = "皮重";
        }
        //件数
        private void Jnum_Click(object sender, EventArgs e)
        {
            NumTypeData = "件数";
        }
        #endregion

        #region 计算机数值点击事件
        //1
        private void uiButton4_Click(object sender, EventArgs e)
        {
            UpNumTypeData(1);
        }
        //2
        private void uiButton13_Click(object sender, EventArgs e)
        {
            UpNumTypeData(2);
        }
        //3
        private void uiButton10_Click(object sender, EventArgs e)
        {
            UpNumTypeData(3);
        }
        //0
        private void uiButton7_Click(object sender, EventArgs e)
        {
            UpNumTypeData(0);
        }
        //4
        private void uiButton15_Click(object sender, EventArgs e)
        {
            UpNumTypeData(4);
        }
        //5
        private void uiButton12_Click(object sender, EventArgs e)
        {
            UpNumTypeData(5);
        }
        //6
        private void uiButton9_Click(object sender, EventArgs e)
        {
            UpNumTypeData(6);
        }
        //.
        private void uiButton6_Click(object sender, EventArgs e)
        {
            UpNumTypeData(".");
        }
        //7
        private void uiButton14_Click(object sender, EventArgs e)
        {
            UpNumTypeData(7);
        }
        //8
        private void uiButton11_Click(object sender, EventArgs e)
        {
            UpNumTypeData(8);
        }
        //9
        private void uiButton8_Click(object sender, EventArgs e)
        {
            UpNumTypeData(9);
        }
        //清零
        private void uiButton5_Click(object sender, EventArgs e)
        {
            UpClearData();
        }
        #endregion
        #region 计算机处理事件
        //添加或减少操作
        public async void UpNumTypeData(object UpNum)
        {
            switch (NumTypeData)
            {
                case "皮重":
                    this.Pnum.Text = this.Pnum.Text == "0" ? "" : this.Pnum.Text;
                    this.Pnum.Text += UpNum.ToString();
                    this.Znum.Text = Math.Round(((Convert.ToDouble(this.ScaleNum.Text) - Convert.ToDouble(this.Pnum.Text)) * (Convert.ToInt32(this.Jnum.Text))), 3).ToString();
                    break;
                case "件数":
                    this.Jnum.Text = this.Jnum.Text == "0" ? "" : this.Jnum.Text;
                    this.Jnum.Text += UpNum.ToString();
                    this.Znum.Text = Math.Round(((Convert.ToDouble(this.ScaleNum.Text) - Convert.ToDouble(this.Pnum.Text)) * (Convert.ToInt32(this.Jnum.Text))), 3).ToString();
                    break;
            };
        }
        //清空操作
        public async void UpClearData()
        {
            switch (NumTypeData)
            {
                case "皮重":
                    this.Pnum.Text = "0";
                    this.Znum.Text = ((Convert.ToDouble(this.ScaleNum.Text) - Convert.ToDouble(this.Pnum.Text)) * (Convert.ToInt32(this.Jnum.Text))).ToString();
                    break;
                case "件数":
                    this.Jnum.Text = "0";
                    this.Znum.Text = ((Convert.ToDouble(this.ScaleNum.Text) - Convert.ToDouble(this.Pnum.Text)) * (Convert.ToInt32(this.Jnum.Text))).ToString();
                    break;
            };
        }
        #endregion
        #endregion

        #region 显示分割称重入库表数据
        int AddNum = 0;
        public async void ShowRKWeighData()
        {
            //http://localhost:5159/api/TSW/ShowRKWeighData
            var res = await HttpClientHelper.RequestUrl<RKWeighData_Now, ApiResult<List<RKWeighData_Now>>>(URLCommon.Url2, "/TSW/ShowRKWeighData", HttpClientHelper.MethodType.GET);
            if (res.Code > 0)
            {
                foreach (var item in res.Data)
                {
                    RKWeighListData.Rows.Add(item.RKWeighId, item.UPCName, item.WeighNum, item.GrossNum, item.furNum, item.suttleNum, item.WeighTime.ToString("yyyy-MM-dd"), "分割仓库");
                }
                if (AddNum == 0)
                {
                    //作废
                    DataGridViewButtonColumn DeleteButton = new DataGridViewButtonColumn();
                    DeleteButton.Text = "作废";
                    DeleteButton.UseColumnTextForButtonValue = true;
                    //修改
                    DataGridViewButtonColumn UpdateButton = new DataGridViewButtonColumn();
                    UpdateButton.Text = "修改";
                    UpdateButton.UseColumnTextForButtonValue = true;
                    //打印
                    DataGridViewButtonColumn ForbidButton = new DataGridViewButtonColumn();
                    ForbidButton.Text = "打印";
                    ForbidButton.UseColumnTextForButtonValue = true;
                    RKWeighListData.Columns.AddRange(DeleteButton, UpdateButton, ForbidButton);
                    AddNum += 1;
                    RKWeighListData.CellClick += new DataGridViewCellEventHandler(ScaleListData_CellContentClick);
                }
            }
        }
        //处理分割称重入库表的表格点击事件
        public async void ScaleListData_CellContentClick(object sender, DataGridViewCellEventArgs e)
        {
            var ButtonDataId = RKWeighListData.Rows[e.RowIndex].Cells[0].Value;

            switch (e.ColumnIndex)
            {
                //删除
                case 8:
                    DelRKWerghtId delRKWerghtId = new(Convert.ToInt32(ButtonDataId));
                    //http://localhost:5159/api/TSW/DelRKWeightData
                    var res = await HttpClientHelper.RequestUrl<DelRKWerghtId, ApiResult<DelRKWerghtId>>(URLCommon.Url1, "/TSW/DelRKWeightData", HttpClientHelper.MethodType.POST, delRKWerghtId);
                    if (res.Code > 0)
                    {
                        MessageBox.Show("删除成功");
                        RKWeighListData.Rows.Clear();
                        ShowRKWeighData();
                    }
                    else
                    {
                        MessageBox.Show("删除失败");
                    }
                    break;
                //修改
                case 9:
                    UpSegmentationData upSegmentationData = new UpSegmentationData(Convert.ToInt32(ButtonDataId));
                    upSegmentationData.Show();
                    break;
                //导出
                case 10:

                    break;
            };
        }
        public record DelRKWerghtId(int RKWeighId);
        //显示分割称重入库表数据
        public class RKWeighData_Now
        {
            /// <summary>
            /// 分割称重入库ID
            /// </summary>
            public int RKWeighId { get; set; }
            /// <summary>
            /// 称重件数
            /// </summary>
            public int WeighNum { get; set; }
            /// <summary>
            /// 毛重
            /// </summary>
            public double GrossNum { get; set; }
            /// <summary>
            /// 皮重
            /// </summary>
            public double furNum { get; set; }
            /// <summary>
            /// 净重
            /// </summary>
            public double suttleNum { get; set; }
            /// <summary>
            /// 称重时间
            /// </summary>
            public DateTime WeighTime { get; set; }
            /// <summary>
            /// 包装名称
            /// </summary>
            public string PackName { get; set; } = string.Empty;
            /// <summary>
            /// 是否删除
            /// </summary>
            public bool IsWeigh { get; set; }
            /// <summary>
            /// 总重
            /// </summary>
            public double SumWeigh { get; set; }
            /// <summary>
            /// 入库数量
            /// </summary>
            public int RKWeighNum { get; set; }
            /// <summary>
            /// 商品代号
            /// </summary>
            public string UPCName { get; set; } = string.Empty;
        }
        #endregion

        #region 添加分割称重
        #region 添加分割称重数据
        //获取包装数据
        List<string> PackageData_now = new List<string>();
        //包装总重量
        string PackageDataZkg = "";
        //称重
        private async void uiButton3_Click(object sender, EventArgs e)
        {
            //实例化类
            RKWeighData rKWeighData = new RKWeighData()
            {
                RKWeighId = 0,
                WeighNum = Convert.ToInt32(this.Jnum.Text),
                GrossNum = Convert.ToDouble(SplitData.SplitRequisGrossWeight),
                furNum = Convert.ToDouble(this.Pnum.Text),
                suttleNum = Convert.ToDouble(this.Znum.Text),
                WeighTime = DateTime.Parse(this.uiTextBox1.Text),
                PackName = string.Join(',', PackageData_),
                IsWeigh = false,
                SumWeigh = Convert.ToDouble(this.Znum.Text),
                RKWeighNum = Convert.ToInt32(this.Jnum.Text),
                UPCName = this.uiButton3.Text,
            };
            //http://localhost:5159/api/TSW/AddRKWeighData
            var res = await HttpClientHelper.RequestUrl<RKWeighData, ApiResult<RKWeighData>>(URLCommon.Url1, "/TSW/AddRKWeighData", HttpClientHelper.MethodType.POST, rKWeighData);
            if (res.Code > 0)
            {
                MessageBox.Show("称重成功");
                RKWeighListData.Rows.Clear();
                ShowRKWeighData();
            }
            else
            {
                MessageBox.Show("称重失败");
            }
        }
        //分割称重入库表字段
        public class RKWeighData
        {
            /// <summary>
            /// 分割称重入库ID
            /// </summary>
            public int RKWeighId { get; set; }
            /// <summary>
            /// 称重件数
            /// </summary>
            public int WeighNum { get; set; }
            /// <summary>
            /// 毛重
            /// </summary>
            public double GrossNum { get; set; }
            /// <summary>
            /// 皮重
            /// </summary>
            public double furNum { get; set; }
            /// <summary>
            /// 净重
            /// </summary>
            public double suttleNum { get; set; }
            /// <summary>
            /// 称重时间
            /// </summary>
            public DateTime WeighTime { get; set; }
            /// <summary>
            /// 包装名称
            /// </summary>
            public string PackName { get; set; } = string.Empty;
            /// <summary>
            /// 是否删除
            /// </summary>
            public bool IsWeigh { get; set; }
            /// <summary>
            /// 总重
            /// </summary>
            public double SumWeigh { get; set; }
            /// <summary>
            /// 入库数量
            /// </summary>
            public int RKWeighNum { get; set; }
            /// <summary>
            /// 商品代号
            /// </summary>
            public string UPCName { get; set; } = string.Empty;
        }
        #endregion
        #region 商品代号点击反填
        //已选择的需要称重的商品
        UPCData SplitData = new UPCData();
        public async void Redioclick(object sender, EventArgs e)
        {
            var ButtonData = sender as Button;
            uiButton3.Text = ButtonData.Text;
            SplitData = uPCDatas.FirstOrDefault(x => x.GoodsMinuteId == (Convert.ToInt32(ButtonData.Name)));
            this.Pnum.Text = SplitData.SplitRequisTares;
            this.Jnum.Text = SplitData.SplitRequisNum;
            this.Znum.Text = SplitData.SplitRequisWeight;
            this.ScaleNum.Text = SplitData.SplitRequisGrossWeight;
        }
        #endregion
        #region 获取分割领用表数据
        List<SplitRequeis> splitRequeis = new List<SplitRequeis>();
        public async void GetSplitRequisData()
        {
            var res = await HttpClientHelper.RequestUrl<SplitRequeis, ApiResult<List<SplitRequeis>>>("", "", HttpClientHelper.MethodType.GET);
            if (res.Code > 0)
            {
                splitRequeis = res.Data;
            }
        }
        //分割领用类
        public class SplitRequeis
        {
            /// <summary>
            /// 分割领用称重Id
            /// </summary>
            public int SplitRequisId { get; set; }

            /// <summary>
            /// 称重时间
            /// </summary>
            public DateTime? SplitRequisTime { get; set; }

            /// <summary>
            /// 仓库
            /// </summary>
            public int DepotId { get; set; }

            /// <summary>
            /// 商品批号
            /// </summary>
            public int ProductId { get; set; }

            /// <summary>
            /// 商品代号
            /// </summary>
            public int MarkId { get; set; }

            /// <summary>
            /// 件数
            /// </summary>
            public string SplitRequisNum { get; set; }

            /// <summary>
            /// 毛重
            /// </summary>
            public string SplitRequisGrossWeight { get; set; }

            /// <summary>
            /// 皮重
            /// </summary>
            public string SplitRequisTares { get; set; }

            /// <summary>
            /// 净重
            /// </summary>
            public string SplitRequisWeight { get; set; }

            /// <summary>
            /// 备注
            /// </summary>
            public string SplitRequisRemark { get; set; }

            /// <summary>
            /// 是否删除
            /// </summary>
            public bool IsDelete { get; set; } = false;
        }
        #endregion
        #endregion


        private void uiButton25_Click(object sender, EventArgs e)
        {
            this.Hide();
        }
    }
}
